function getJsonLength(jsonData){
    var jsonLength = 0;
    for(var item in jsonData){
        jsonLength++;
    }
    return jsonLength;
}


var gift = {
    LeaveTimer:null,
    showTimer:null,
    calculateTimer:null,
    timeStamp:"159454145_0_2",
    isL:true,
    isL1:true,
    _num: 0,
    showQueue:{ // 礼物显示队列
    },
    ElemlQueue:{ // 礼物元素显示队列
    },
    giftQueue:{  // 礼物等待队列
    },
    init:function () { // 初始化
         // 清楚所有队列
         this.showQueue = {};
         this.giftQueue = {};
    },
    addgiftToQueue:function (data,isSocket) {  // 添加礼物至队列
         var nowdate= parseInt((new Date())/1000);
         var nowdate15s = nowdate+15;
         var uid = data['data']['uid'];
         var gid = data['data']['gid'];
         var timestamp = this.timeStamp;
         var _timestampArr = timestamp.split("_");
         istimestamp = _timestampArr[0]+"_"+uid+"_"+gid;
         // 判断礼物是否在显示中
         if (this.showQueue.hasOwnProperty(istimestamp)){
             if (nowdate<=_timestampArr[0]) {
                 if (this.isDouClick()){
                     this._num = this._num+1;
                 }else{
                     this._num = this.showQueue['' + istimestamp + '']['num'];
                 }
                 document.getElementById('gift_' + istimestamp).getElementsByTagName('b')[0].innerHTML = this._num;
                 this.showQueue[istimestamp]['num'] = this._num;
                 return;
             }
         }
             // 添加到等待队列中

             // step1 判断等待队列中是否存在该礼物
             if (this.giftQueue.hasOwnProperty(istimestamp)){
                 if (nowdate<=_timestampArr[0]) {
                     if (this.isDouClick()){
                         this._num = this._num+1;
                         console.log(111111);
                     }else{
                         this._num = this.giftQueue['' + istimestamp + '']['num']+1;
                         console.log(222222);
                     }
                     var _num = this.giftQueue['' + istimestamp + '']['num'];
                     this.giftQueue['' + istimestamp + '']['num'] = this._num;
                     return;
                 }
             }

             // step2 该队列中没有则添加新礼物
             data['time'] = nowdate15s+"_"+uid+"_"+gid;
             this.giftQueue[nowdate15s+"_"+uid+"_"+gid] = data;
             this.timeStamp = nowdate15s+"_"+uid+"_"+gid;
             dataMess = '{"type":"sendGift","data":{"gname":"'+data['data']['gname']+'","gift":"'+data['data']['gift']+'","uid":"'+data['data']['uid']+'","gid":"'+data['data']['gid']+'","username":"'+data['data']['username']+'"},"num":"1","shownum":"1","time":"15","room":"'+room+'"}';
             if (isSocket){
                 ws.send(dataMess);
             }
        if (this.isL1){
                 this.showGiftToQueue();
                 this.isL1 = false;
             }

             if (this.isL){
                  this.LeaveFromshowQueue();
                  this.isL = false;
             }
    },
    showGiftToQueue:function () {
        _this = this;
        // 如果当前礼物队列为空 清空定时器
        if (getJsonLength(_this.giftQueue)==0) {
            clearInterval(_this.showTimer);
            _this.isL1 = true;
            return;
        }
        // 判断当前显示列表是否存在 3 个礼物队列
        $showQueue = getJsonLength(_this.showQueue);
        $showElemQueue = getJsonLength(_this.ElemlQueue);
        if ($showQueue < 2 && $showElemQueue < 2) {  // 必须两者同时满足
            // 读取 礼物等待队列的第一个
            _gift = _this.getFirstDataByJson(_this.giftQueue);
            _k = _this.getFirstKeyByJson(_this.giftQueue);

            // 添加到礼物显示队列中
            _this.showQueue[_k]=_gift;

            // 从礼物等待对列中删除
            delete _this.giftQueue[_k];

            // 计算显示位置 显示依据显示礼物队列中的个数乘以每个礼物区域的高
            var giftshowtop = getJsonLength(_this.showQueue)*60;
            _gift.showtop = giftshowtop;
            _this.ElemlQueue[_k] = _gift;

            // 生成HTML
            $_html = _this.giftHtml(_gift);
            // 添加到DOM中
            $('#danmbox').append($_html);
        }
        this.showTimer = setInterval(function () {
             // 如果当前礼物队列为空 清空定时器
             if (getJsonLength(_this.giftQueue)==0) {
                   clearInterval(_this.showTimer);
                   _this.isL1 = true;
                   return;
             }
             // 判断当前显示列表是否存在 3 个礼物队列
             $showQueue = getJsonLength(_this.showQueue);
             if ($showQueue < 2 ) {
                  // 读取 礼物等待队列的第一个
                  _gift = _this.getFirstDataByJson(_this.giftQueue);
                  _k = _this.getFirstKeyByJson(_this.giftQueue);

                  // 添加到礼物显示队列中
                  _this.showQueue[_k]=_gift;

                  // 从礼物等待对列中删除
                  delete _this.giftQueue[_k];

                  // 计算显示位置 显示依据显示礼物队列中的个数乘以每个礼物区域的高
                  var giftshowtop = getJsonLength(_this.showQueue)*60;
                  _gift.showtop = giftshowtop;

                  // 生成HTML
                  $_html = _this.giftHtml(_gift);
                  // 添加到DOM中
                  $('#danmbox').append($_html);
             }
        },600);
    },
    isDouClick:function() {
        var clickStamp = new Date().getTime(); // 点击的时间戳
        lastCLickTime = localStorage.getItem('clickTimes');
        localStorage.setItem('clickTimes',clickStamp);
        if (clickStamp-lastCLickTime<500){
            console.log('连击');
            return true;
        }else{
            console.log('非连击')
            return false;
        }
    },
    createJson:function(pop,val,json){
            // 如果 val 被忽略
            if(typeof val === "undefined") {
                // 删除属性
                delete json[prop];
            }
            else {
                // 添加 或 修改
                json[prop] = val;
            }
    },
    LeaveFromshowQueue:function () { // 从显示队列中删除 显示连击特效
         clearInterval(this.LeaveTimer);
         _this = this;
         this.LeaveTimer = setInterval(function () {
              if (getJsonLength(_this.showQueue)==0){
                  clearInterval(_this.LeaveTimer);
                  _this.isL = true;
                  _isClose = true;
              }
              for (d3 in _this.showQueue) {
                   if (_this.showQueue[d3].shownum==_this.showQueue[d3].num+1){
                       _index = d3;
                       document.getElementById('lianji_'+_index).className = 'lianjied';
                       delete _this.showQueue[_index]; // 从显示队列中删除
                       setTimeout(function () {
                            if (document.getElementById('gift_'+_index)){
                             document.getElementById('gift_'+_index).className = 'gift_show animated bounceOutUp';
                             _rbox = document.getElementById('gift_'+_index);
                             delete _this.ElemlQueue[_index]; // 从显示元素队列中删除
                             setTimeout(function () {
                                 document.getElementById('danmbox').removeChild(_rbox);
                             },300);
                             _this.recalculatePosition();
                            }
                        },1000);
                   }else{ // 连击特效
                        _iNow = d3;
                        _gbox = document.getElementById('gift_'+_iNow);
                        _gboxlianji = _gbox.getElementsByClassName("lianji")[0];
                        _gboxspan = _gboxlianji.getElementsByTagName('span')[0];
                        _gboxspan.innerHTML = _this.showQueue[_iNow]['shownum'];
                        _this.showQueue[d3]['shownum'] =  _this.showQueue[_iNow]['shownum']+1;
                        $(".lianji").transition({scale: 1.5}, 200, 'linear', function(){
                             $(".lianji").css({scale: 1});
                        });
                   };
              }
         },600)
    },
    recalculatePosition:function () { // 重新计算位置
         if (getJsonLength(this.showQueue)==0){
              return;
         }
         var _topIndex = 1;
         var _toph = 60;
         for(d4 in this.showQueue) {
              var doc = document.getElementById('gift_'+d4);
              doc.style.top = _topIndex*_toph+'px';
              _topIndex+1;
         }
    },
    getFirstDataByJson:function (data) { // 从json 数组中返回第一个
         for (d in data){
              return data[d];
         }
    },
    getFirstKeyByJson:function (data) { // 从json 数组中返回第一个
        for (d in data){
            return d;
        }
    },
    giftHtml:function (data) {  // 生成礼物展示显示模板
        $temp = '';
        $temp += '<div class="gift_show" id="gift_'+data['time']+'" style="top: '+data['showtop']+'px">'
        $temp +=   '<div class="gift_inner">'
        $temp +=   '<div class="gift_face">'
        $temp +=   '<img src="'+data['data']['upath']+'" alt="">'
        $temp +=   '</div>'
        $temp +=   '<div class="gift_info">'
        $temp +=   '<p class="u">'+data['data']['username']+'</p>'
        $temp +=   '<p class="gift_desc">对主播送出了<b>'+data['num']+'</b> '+data['data']['gname']+' </p>'
        $temp +=   '<div class="gift_img">'
        $temp +=   '<img src="'+data['data']['gift']+'" alt="">'
        $temp +=   '</div>'
        $temp +=   '</div>'
        $temp +=   '</div>'
        $temp +=   '<div class="lianji" id="lianji_'+data['time']+'">'
        $temp +=   '<i></i> <span>1</span>'
        $temp += '</div>'
        $temp += '</div>';
        return $temp;
    }
}

